<?php

namespace App\Admin\Controllers;

use App\Models\Brand;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Encore\Admin\Widgets\Table;


class BrandController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '品牌';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Brand);

        $grid->column('id', __('Id'));
        $grid->brandtype()->name(__('品牌类型'));
        $grid->column('name', __('品牌名称'));
        // ->expand(function ($model) {

        //     $comments = $model->where('id',$this->id)->get()->map(function ($comment) {
        //         return $comment->only(['id', 'desc']);
        //     });

        //     return new Table(['ID', '品牌描述'], $comments->toArray());
        // });
        $grid->column('sort', __('排序'));
        $grid->column('comment', __('备注'));
        $grid->column('created_at', __('创建时间'));
        $grid->column('updated_at', __('修改时间'));

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Brand::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('type', __('Type'));
        $show->field('name', __('Name'));
        $show->field('sort', __('Sort'));
        $show->field('desc', __('Desc'));
        $show->field('comment', __('Comment'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Brand);

        $form->select('type', __('品牌类型'))->options('/admin/brandtypes/get_brand_type_list');
        $form->text('name', __('品牌名称'));
        // $form->ckeditor('desc', __('品牌描述'));
        $form->editor('desc', __('品牌描述'));
        $form->number('sort', __('排序'))->default(0);
        $form->text('comment', __('备注'))->default('无');

        return $form;
    }

    public function get_brand_list()
    {
        $list = Brand::get(['id','name as text']);
        $list = $list->toArray();
        return $list;
    }
    public function get_brand()
    {
        $list = Brand::get(['id','name']);
        $list = array_column($list->toArray(),'name','id');
        return $list;
    }
    
}
